package com.money.mvp_dagger_simple.model;

import com.money.mvp_dagger_simple.http.HttpUtil;
import com.money.mvp_dagger_simple.http.api.Api;
import com.money.mvp_dagger_simple.http.listener.MRequestListener;
import com.money.mvp_dagger_simple.model.reqparams.IRequestParams;
import com.money.mvp_dagger_simple.presenter.impl.ILoginPresenterImpl;

import java.util.Map;

import javax.inject.Inject;

/**
 * Created by Money on 2017/8/28.
 */

public class LoginModel implements MRequestListener{

    private HttpUtil httpUtil;
    private ILoginPresenterImpl presenter;
    private IRequestParams requestParams;

    @Inject
    public LoginModel(HttpUtil httpUtil, ILoginPresenterImpl presenter, IRequestParams requestParams) {
        this.httpUtil = httpUtil;
        this.presenter = presenter;
        this.requestParams = requestParams;
    }

    public void login(String acount, String password){
        Map<String, Object> map = requestParams.getLoginParams(acount, password);
        httpUtil.post(this, Api.LOGIN, map, "");
    }

    @Override
    public void onSuccess(String url, String response, Object reqTag, String... byValue) {
        if (url.equals(Api.LOGIN)){
            presenter.loginSuccess();
        }
    }

    @Override
    public void onError(String url, String response, Object reqTag, String... byValue) {
        if (url.equals(Api.LOGIN)){
            presenter.loginFailure();
        }
    }
}
